//
//  YHReachabilityManager.m
//  YHNetwork
//
//  Created by 000 on 2021/12/25.
//

#import "YHReachabilityManager.h"

NSString * YHNetworkStatusDescription(YHNetworkStatus status){
    if(status == YHNetworkStatusUnknown)        return @"未识别的网络";
    if(status == YHNetworkStatusNotReachable)   return @"不可达的网络(未连接)";
    if(status == YHNetworkStatusReachableViaWWAN) return @"数据流量";
    if(status == YHNetworkStatusReachableViaWiFi) return @"Wifi";
    return @"未识别的网络";
}

@implementation YHReachabilityManager

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 网络状态监听
        [YHReachabilityManager reachabilityStartMonitoring];
    });
}
/** 网络监听 */
+ (void)reachabilityStartMonitoring{
    AFNetworkReachabilityManager * manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
}

/// 网络变化
+ (void)networkChangeBlock:(void(^)(YHNetworkStatus status, NSString * description))block{
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if(block){
            block((YHNetworkStatus)status, YHNetworkStatusDescription((YHNetworkStatus)status));
        }
    }];
}
+ (YHNetworkStatus)networkReachabilityStatus{
    return (YHNetworkStatus)[AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
}
+ (BOOL)reachable{
    return [AFNetworkReachabilityManager sharedManager].reachable;
}
+ (BOOL)reachableViaWWAN{
    return [AFNetworkReachabilityManager sharedManager].reachableViaWWAN;
}
+ (BOOL)reachableViaWiFi{
    return [AFNetworkReachabilityManager sharedManager].reachableViaWiFi;
}


@end
